<!DOCTYPE html>
<html>
<head>
    <title>Circuit Diagram Fig. 3.21</title>
</head>
<body>
    <canvas id="circuitCanvas" width="600" height="450"></canvas>
    <script>
        const canvas = document.getElementById('circuitCanvas');
        const ctx = canvas.getContext('2d');

        // Style settings to mimic a hand-drawn look
        ctx.strokeStyle = 'black';
        ctx.fillStyle = 'black';
        ctx.lineWidth = 2.5;
        ctx.font = 'bold 22px Arial';
        ctx.textAlign = 'center';
        ctx.textBaseline = 'middle';

        // Helper function to draw a resistor
        function drawResistor(x_start, y, width, height) {
            const x = x_start;
            const w = width;
            const h = height;
            ctx.beginPath();
            ctx.moveTo(x, y);
            ctx.lineTo(x + w * 0.1, y);
            ctx.lineTo(x + w * 0.25, y - h);
            ctx.lineTo(x + w * 0.5, y + h);
            ctx.lineTo(x + w * 0.75, y - h);
            ctx.lineTo(x + w * 0.9, y);
            ctx.lineTo(x + w, y);
            ctx.stroke();
        }

        // Helper function to draw an arrow
        function drawArrow(x1, y1, x2, y2, size) {
            ctx.beginPath();
            ctx.moveTo(x1, y1);
            ctx.lineTo(x2, y2);
            const angle = Math.atan2(y2 - y1, x2 - x1);
            ctx.moveTo(x2, y2);
            ctx.lineTo(x2 - size * Math.cos(angle - Math.PI / 6), y2 - size * Math.sin(angle - Math.PI / 6));
            ctx.moveTo(x2, y2);
            ctx.lineTo(x2 - size * Math.cos(angle + Math.PI / 6), y2 - size * Math.sin(angle + Math.PI / 6));
            ctx.stroke();
        }

        // --- Define coordinates ---
        const y_top = 120;
        const y_bottom = 320;
        const x_left_branch = 130;
        const x_node1 = 290;
        const x_node2_A = 450;
        const resistor_width = 80;
        const resistor_height = 12;
        const capacitor_plate_width = 40;
        const capacitor_gap = 10;
        const y_cap_top = 190;
        const y_cap_bottom = y_cap_top + capacitor_gap;

        // --- Draw Wires and Components ---

        // Bottom wire
        ctx.beginPath();
        ctx.moveTo(x_left_branch, y_bottom);
        ctx.lineTo(x_node2_A, y_bottom);
        ctx.stroke();

        // Left branch with switch
        ctx.beginPath();
        ctx.moveTo(x_left_branch, y_bottom);
        ctx.lineTo(x_left_branch, 230); // Wire up to switch
        ctx.moveTo(x_left_branch, 170); // Wire from switch up
        ctx.lineTo(x_left_branch, y_top);
        ctx.stroke();

        // Switch
        ctx.beginPath();
        ctx.moveTo(x_left_branch, 230);
        ctx.lineTo(x_left_branch - 30, 180);
        ctx.stroke();

        // Top wire
        ctx.beginPath();
        ctx.moveTo(x_left_branch, y_top);
        ctx.lineTo(x_node1 - resistor_width / 2, y_top);
        ctx.moveTo(x_node1 + resistor_width / 2, y_top);
        ctx.lineTo(x_node2_A - resistor_width / 2, y_top);
        ctx.moveTo(x_node2_A + resistor_width / 2, y_top);
        ctx.lineTo(x_node2_A, y_top); // Connect to node A
        ctx.stroke();

        // Resistors
        drawResistor(x_node1 - resistor_width / 2, y_top, resistor_width, resistor_height);
        drawResistor(x_node2_A - resistor_width / 2 - 20, y_top, resistor_width, resistor_height);

        // Middle branch with capacitor C1
        ctx.beginPath();
        ctx.moveTo(x_node1, y_top);
        ctx.lineTo(x_node1, y_cap_top);
        ctx.moveTo(x_node1, y_cap_bottom);
        ctx.lineTo(x_node1, y_bottom);
        ctx.stroke();
        // Capacitor C1 plates
        ctx.beginPath();
        ctx.moveTo(x_node1 - capacitor_plate_width / 2, y_cap_top);
        ctx.lineTo(x_node1 + capacitor_plate_width / 2, y_cap_top);
        ctx.moveTo(x_node1 - capacitor_plate_width / 2, y_cap_bottom);
        ctx.lineTo(x_node1 + capacitor_plate_width / 2, y_cap_bottom);
        ctx.stroke();

        // Right branch with capacitor C2
        ctx.beginPath();
        ctx.moveTo(x_node2_A, y_top);
        ctx.lineTo(x_node2_A, y_cap_top);
        ctx.moveTo(x_node2_A, y_cap_bottom);
        ctx.lineTo(x_node2_A, y_bottom);
        ctx.stroke();
        // Capacitor C2 plates
        ctx.beginPath();
        ctx.moveTo(x_node2_A - capacitor_plate_width / 2, y_cap_top);
        ctx.lineTo(x_node2_A + capacitor_plate_width / 2, y_cap_top);
        ctx.moveTo(x_node2_A - capacitor_plate_width / 2, y_cap_bottom);
        ctx.lineTo(x_node2_A + capacitor_plate_width / 2, y_cap_bottom);
        ctx.stroke();

        // Ground symbol
        ctx.beginPath();
        ctx.moveTo(x_node2_A, y_bottom);
        ctx.lineTo(x_node2_A, y_bottom + 20);
        ctx.moveTo(x_node2_A - 20, y_bottom + 20);
        ctx.lineTo(x_node2_A + 20, y_bottom + 20);
        ctx.stroke();

        // Terminal A
        ctx.beginPath();
        ctx.arc(x_node2_A, y_top, 4, 0, 2 * Math.PI);
        ctx.fill();


        // --- Labels and Annotations ---
        const label_font = 'bold 24px Arial';
        const symbol_font = 'bold 20px Arial';
        const sub_font = 'italic bold 18px Arial';

        // Resistor labels
        ctx.font = label_font;
        ctx.fillText('R', x_node1, y_top - 30);
        ctx.fillText('R', x_node2_A - 20, y_top - 30);

        // Capacitor labels
        ctx.fillText('C', x_node1 - 40, y_cap_top + capacitor_gap / 2);
        ctx.fillText('C', x_node2_A - 40, y_cap_top + capacitor_gap / 2);

        // Voltage labels
        ctx.font = symbol_font;
        ctx.fillText('+', x_node1 + 25, y_cap_top - 5);
        ctx.fillText('-', x_node1 + 25, y_cap_bottom + 5);
        ctx.font = label_font;
        ctx.fillText('v', x_node1 + 45, y_cap_top + capacitor_gap / 2);
        ctx.font = sub_font;
        ctx.fillText('1', x_node1 + 55, y_cap_top + capacitor_gap / 2 + 5);

        ctx.font = symbol_font;
        ctx.fillText('+', x_node2_A + 25, y_cap_top - 5);
        ctx.fillText('-', x_node2_A + 25, y_cap_bottom + 5);
        ctx.font = label_font;
        ctx.fillText('v', x_node2_A + 45, y_cap_top + capacitor_gap / 2);
        ctx.font = sub_font;
        ctx.fillText('2', x_node2_A + 55, y_cap_top + capacitor_gap / 2 + 5);

        // Current labels and arrows
        ctx.font = label_font;
        ctx.textAlign = 'center';
        // i1
        drawArrow(x_node1 - 35, y_top + 20, x_node1 + 35, y_top + 20, 8);
        ctx.font = sub_font;
        ctx.fillText('i', x_node1 - 5, y_top + 40);
        ctx.fillText('1', x_node1, y_top + 45);
        // i2
        drawArrow(x_node2_A - 55, y_top + 20, x_node2_A + 15, y_top + 20, 8);
        ctx.fillText('i', x_node2_A - 25, y_top + 40);
        ctx.fillText('2', x_node2_A - 20, y_top + 45);
        // i3
        drawArrow(x_node1, y_bottom - 60, x_node1, y_bottom - 20, 8);
        ctx.fillText('i', x_node1 + 25, y_bottom - 45);
        ctx.fillText('3', x_node1 + 30, y_bottom - 40);

        // Point A label
        ctx.font = label_font;
        ctx.fillText('A', x_node2_A + 15, y_top - 15);

        // Figure caption
        ctx.font = 'bold 20px Arial';
        ctx.fillText('Fig. 3.21', canvas.width / 2, 420);
    </script>
</body>
</html>